<?php
/************************************
*   熊店
*   @file:/server/bearshop/__admin/__global.php
*   @intro:
*   @author:new_shop
*   @email:new_shop@163.com    
*   @date:Tue Feb 09 22:47:22 CST 2010
************************************/


define('K5_STARTED' , true);
define('K5_ROOT' , dirname(dirname(__FILE__)));

include(K5_ROOT.'/__include/sysdefine.php');
include(K5_INCLUDE_PATH.'/k.php');
include(K5_INCLUDE_PATH.'/function.php');
include(K5_CONFIG_PATH.'/config.php');
include(K5_CONFIG_PATH.'/admin_config.php');
include(K5_CONFIG_PATH.'/statuscode.php');

ini_set('DISPLAY_ERRORS' , 1);
//error_reporting(E_ALL & ~E_NOTICE);

define('K_ADMCODE_NO_LOGIN' , 'ADM00001');
$adm = array();

class admin_ctrl
{
    private $session;
    private $session_data = array();
    protected $need_login = true;
    protected $module_data = array();
    
    public function __construct()
    {
        $this->session = K::session();
        $this->session->get('__ADMIN' , $this->session_data);
        
        
        if(!isset($this->session_data['uid']) && $this->session_data['uid'] < 1 && $this->need_login)
        {
            
            $this->no_login();
        }
        
        $this->_construct();
        
        if($_GET['page'])
        {
            $method = 'page_'.$_GET['page'];
            if(method_exists($this , $method))
            {
                $this->$method();
            }
            else
                $this->_redirect();
        }
        else if($_GET['api'])
        {
            $method = 'api_'.$_GET['api'];
            if(method_exists($this , $method))
            {
                $this->$method();
            }
        }
        else 
        {
            
            $this->run();
        }
        
        
        
        return ;
    }
    /**
    * 准构造函数 需要在子类中被定义 在RUN之前执行
    * 
    */
    protected function _construct()
    {
    
        
    }
    
    /**
    * put your comment there...
    * 
    * @param mixed $key
    * @param mixed $value
    */
    protected function set_session($key , $value)
    {
     
        $this->session_data[$key] = $value;
        $this->session->set('__ADMIN' , $this->session_data);
    }
    public  function get_session($key)
    {
        return $this->session_data[$key];
    }
    protected function del_session($key)
    {
        unset($this->session_data[$key]);
        $this->session->set('__ADMIN' , $this->session_data);
    }
    
    /**
	 * get the input
	 * caution!! could not get the array like this : ?a[]=1&a[]=2&a[]=3
	 *
	 * @param string $key
	 * @param string $method  G/GET P/POST OTHER
	 * @param mix $default	//default value when !isset
	 * @return mix
	 */
	protected function input($key , $method = 'all' , $default = null)
	{
		$method = strtoupper($method);
		if('G' == $method || 'GET' == $method)
        {
            $value = isset($_GET[$key]) ? $_GET[$key] : $default;
            $this->__input[$key] = trim($value);
        }
        if('P' == $method || 'POST' == $method)
        {
            $value = isset($_POST[$key]) ? $_POST[$key] : $default;
            $this->__input[$key] = trim($value);
        }
        else
        {
            $value = isset($_GET[$key]) 
                    ? $_GET[$key] 
                    : (isset($_POST[$key]) 
                        ? $_POST[$key] 
                        : $default);
            $this->__input[$key] = trim($value);
        }
        
        return trim($value);
	}
	
    protected function output_js($code , $data)
    {
        echo json_encode(array('code'=>$code , 'data'=>$data));
        die;
    }
    protected function output($data = array())
    {
        global $adm , $_url;
        $adm = array_merge($data , $this->module_data);
        $tpl_file = 'page_'.get_class($this);
        $page = $_GET['page'] ? 'page_'.$_GET['page'] : 'page_index';
        echo '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录管理后台</title>
<link href="../css.css" rel="stylesheet" type="text/css">
</head>
<body>';
        
        if(is_file(K5_ADMIN_PATH.'/view/'.$tpl_file.'.php'))
        {
            include(K5_ADMIN_PATH.'/view/'.$tpl_file.'.php');
            
            $page($adm);
        }
        else
            die('tpl_error');
        echo '</body>
</html>';
        die;
    }
    
    protected function run(){}
    /**
     * 未登录 跳转到登录页面
     *
     */
    protected function no_login()
    {
        echo '<script>window.top.location="'.K5_ADMIN_DIR.'/login.php";</script>';
        die;
    }
    protected function busy($msg = '')
    {
        die('BUSY!'.$msg);
    }
    
    protected function _redirect($url , $lazy_msg = '' , $sec = 1)
    {
        if(headers_sent() || $lazy_msg)
        {
            echo '<meta http-equiv="refresh" content="'.$sec.'; url='.$url.'" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
            echo $lazy_msg.'<br/>'.$sec.'秒后跳转...';
            die;
        }
        else 
        {
            header('Location:'.$url);
            die;
        }
    }
    protected function _parent_reload()
    {
        echo "<script>window.parent.histrory.reload();</script>";
    }
    protected function back()
    {
        $this->_redirect($_SERVER['HTTP_REFERER']);
    }
    
    protected function _check_adm_level($access)
    {
    	$level = $this->get_session('level');
    	if($level == K_ADM_LV_ADMINISTRATOR)
    		return true;
    		
    	if($level != $access)
    		die('该功能只对管理员开放');
    }
}




?>